//
//  main.m
//  05- 循环遍历
//
//  Created by mafeng on 16/7/8.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
    NSArray *array = [NSArray arrayWithObjects:@"马峰", @"黎明", @"太阳", @"月亮", @"上山", nil];
    NSLog(@"%@", array);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"马峰", @"name", @"男", @"sex", @12, @"age", nil];
    for (NSUInteger i = 0; i < dict.allKeys.count; i++) {
        NSArray *keys = [dict allKeys];
    }
     // 枚举器
     // NSEnumerator依赖于集合类型(数组, 字典, 集合), 不能单独使用
        NSEnumerator *arrayEnumerator = [array objectEnumerator];
        id value = nil;
        while (value = [arrayEnumerator nextObject]) {
            NSLog(@"%@", value);
        }
        
        // 倒序枚举
        NSEnumerator *reverse = [array reverseObjectEnumerator];
        id value1 = nil;
        while (value1 = [reverse nextObject]) {
            NSLog(@"%@", value1);
        }
    
       // 字典枚举器(字典无序, 所以没有倒序)
       // 字典枚举器默认打印出来的值是value
    NSEnumerator *dictEnumerator = [dict objectEnumerator];
    id dictValue = nil;
    while (dictValue = [dictEnumerator nextObject]) {
        NSLog(@"%@", dictValue);
    }
    
      // 集合枚举
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"867", @"55", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"%@", setValue);
    }
    
    // 快速枚举 for....in... 是对枚举器的一种封装
    for (NSString *string in array) {
        NSLog(@"%@", string);
    }
    NSMutableArray *mutableArray = [NSMutableArray array];
    NSString *str = [NSString string];
    for (NSString *string in array) {
        [mutableArray addObject:string];
        str = [str stringByAppendingString:string];
    }
    NSLog(@"字符串:%@", str);
    NSLog(@"%@", mutableArray);
    
    // 快速枚举一个字典
    for (NSString *key in dict) {
        NSLog(@"%@", key);
    }
    // 快速枚举一个集合
    for (NSString *anyObject in set) {
        NSLog(@"anyObject = %@", anyObject);
    }
    
    // 排序
    // 参数一: key: 排序条件
    // 参数二: acending: YES(升序) or NO(降序)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    // 不可变数组有排序的方法(sortedArrayUsingDescriptors), 生成一个新的数组
    // 参数为一个排序条件的数组
    NSArray *sortArray = [array sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", sortArray);
    
    // 可变数组排序时, 使用sortUsingDescriptors, 在自己本身上排序, 不用用数组去接收
    [mutableArray sortUsingDescriptors:@[sortDescriptor]];
    
    Student *stu1 = [[Student alloc] init];
    stu1.name = @"好难";
    Student *stu2 = [[Student alloc] init];
    stu2.name = @"真的好难";
    Student *stu3 = [[Student alloc] init];
    stu3.name = @"是啊,好难";
    NSArray *stuArray = @[stu1, stu2, stu3];
    // 根据name去排序
    NSSortDescriptor *stuSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    stuArray = [stuArray sortedArrayUsingDescriptors:@[stuSort]];
    
    // 排序2
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch"];
    NSLog(@"%@", sortString);
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
     NSLog(@"%@", sortString);
    
    // for in 后面可以加顺序和倒序的枚举器
    return 0;
    
}
